
goog.provide('esn.PersonalSurvey');

goog.require('esn.BaseSurvey');
goog.require('esn.PersonalSurveyQuestions');
goog.require('esn.Question');
goog.require('esn.constants');
goog.require('esn.model.QuestionModel');
goog.require('esn.soy.personalSurvey');
goog.require('goog.array');
goog.require('goog.object');
goog.require('goog.style');
goog.require('soy');


/**
 * @constructor
 */
esn.PersonalSurvey = function(appContext, opt_domHelper) {
  goog.base(this, appContext, esn.PersonalSurvey.SECTION_ID, opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = appContext.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);

  this.questions_ = esn.PersonalSurveyQuestions;
};
goog.inherits(esn.PersonalSurvey, esn.BaseSurvey);

esn.PersonalSurvey.EventType = {
  BACK: goog.events.getUniqueId('back'),
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.PersonalSurvey.SECTION_ID = 'personal';

esn.PersonalSurvey.prototype.questions_;

esn.PersonalSurvey.prototype.continueButton_;

esn.PersonalSurvey.prototype.backButton_;

esn.PersonalSurvey.prototype.createDom = function() {
  goog.base(this, 'createDom');

  this.setElementInternal(soy.renderAsElement(esn.soy.personalSurvey.content,
      {QuestionList: this.questions_}));

  this.continueButton_ = new goog.ui.CustomButton(
      esn.constants.getContinueButtonCaption());
  this.continueButton_.render(this.getElementByClass('continueButton'));

  this.backButton_ = new goog.ui.CustomButton(
      esn.constants.getBackButtonCaption());
  this.backButton_.render(this.getElementByClass('backButton'));

  this.surveyClient_.getSectionAnswers(esn.PersonalSurvey.SECTION_ID,
      goog.bind(this.getSectionAnswersResponse_, this));
};

esn.PersonalSurvey.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  this.getHandler().listen(
      this.backButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleBackClick_);
};

esn.PersonalSurvey.prototype.getQuestions = function() {
  return this.questions_ || [];
};

esn.PersonalSurvey.prototype.getSectionAnswersResponse_ = function(response) {
  var answerMap = this.createAnswerMap(response);
  this.questions_ = this.createSectionQuestions(this.getElement(),
      this.questions_, answerMap);

  // Update progress bar.
  this.handleQuestionChange();
};

esn.PersonalSurvey.prototype.handleContinueClick_ = function(e) {
  if (this.validateQuestions()) {
    this.dispatchEvent(esn.PersonalSurvey.EventType.CONTINUE);
  }
};

esn.PersonalSurvey.prototype.handleBackClick_ = function(e) {
  this.dispatchEvent(esn.PersonalSurvey.EventType.BACK);
};
